<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="图标" prop="icon">
        <el-input
          v-model="form.icon"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入图标"
        ></el-input>
      </el-form-item>
      <el-form-item label="Title" prop="title">
        <el-input
          v-model="form.title"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入Title"
        ></el-input>
      </el-form-item>
      <el-form-item label="图标颜色" prop="color">
        <el-color-picker
          v-model="form.color"
          size="mini"
          style="padding-top: 10px"
        ></el-color-picker>
      </el-form-item>
      <el-form-item label="社交方式" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">链接</el-radio>
          <el-radio :label="2">图片</el-radio>
          <el-radio :label="3">文字</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="社交链接" prop="href" v-if="form.type === 1">
        <el-input
          clearable
          v-model.trim="form.href"
          size="mini"
          style="width: 600px"
          type="textarea"
          :rows="2"
          maxlength="255"
          show-word-limit
          placeholder="请输入社交链接"
        />
      </el-form-item>
      <el-form-item label="文字说明" prop="text" v-if="form.type === 3">
        <el-input
          clearable
          v-model.trim="form.text"
          size="mini"
          style="width: 600px"
          type="textarea"
          :rows="2"
          maxlength="200"
          show-word-limit
          placeholder="请输入文字说明"
        />
      </el-form-item>
      <el-form-item label="社交图片" prop="img" v-if="form.type === 2">
        <el-input
          clearable
          v-model.trim="form.img"
          size="mini"
          style="width: 600px"
          type="textarea"
          :rows="3"
          maxlength="255"
          show-word-limit
          placeholder="请输入在线图片地址"
        />
        <mk-upload v-model="form.img" />
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";
import {
  dirSocialsCreate,
  dirSocialsUpdate,
} from "@/api/modules/sys.socials.api";
import MkUpload from "@/components/common/mk-upload";

export default {
  name: "get-socials-info",
  components: { MkUpload, MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {
        type: 1,
      },
      isAdd: true,
      rules: {
        icon: [{ required: true, message: "请输入图标", trigger: "blur" }],

        color: [{ required: true, message: "请输入图标颜色", trigger: "blur" }],

        href: [{ required: true, message: "请输入社交链接", trigger: "blur" }],

        text: [
          { required: true, message: "请输入社交文字说明", trigger: "blur" },
        ],

        img: [{ required: true, message: "请输入社交图片", trigger: "blur" }],

        createdAt: [
          { required: true, message: "请输入创建时间", trigger: "blur" },
        ],

        updatedAt: [
          { required: true, message: "请输入修改时间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
        console.log(data);

        this.$set(
          this.form,
          "type",
          data.href ? 1 : data.img ? 2 : data.text ? 3 : 0
        );
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          if (this.form.type == 1) {
            this.form.img = null;
            this.form.text = null;
          } else if (this.form.type == 2) {
            this.form.href = null;
            this.form.text = null;
          } else {
            this.form.href = null;
            this.form.img = null;
          }
          _FUC = this.type == "create" ? dirSocialsCreate : dirSocialsUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {};
    },
  },
};
</script>

<style lang="scss" scoped></style>
